package com.lc.cehui.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CompanyNaturalPublistApi;
import com.lc.cehui.http.response.CompanyNaturalPublistBean;
import com.lc.cehui.other.KeyboardWatcher;
import com.lc.cehui.ui.activity.WebVIewActivity;
import com.lc.cehui.widget.ClearEditText;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class IntelligenceSecondActivity extends AppActivity implements KeyboardWatcher.SoftKeyboardStateListener,
        TextView.OnEditorActionListener{
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    BaseQuickAdapter<CompanyNaturalPublistBean.DataBean, BaseViewHolder> adapter;
    List<CompanyNaturalPublistBean.DataBean> datelist = new ArrayList<>();
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.et_search)
    ClearEditText etSearch;
    private int mPage = 1;
    private String flag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_intelligencesecond;
    }

    @Override
    protected void initView() {
        titleBar.setTitle(getIntent().getStringExtra("title"));
        company_natural_publist();

    }

    private void company_natural_publist() {
        EasyHttp.post(this)
                .api(new CompanyNaturalPublistApi().setPage(mPage).setType_id(getIntent().getStringExtra("id")).setKey(etSearch.getText().toString()))
                .request(new HttpCallback<CompanyNaturalPublistBean>(this) {

                    @Override
                    public void onSucceed(CompanyNaturalPublistBean data) {

                        if (data.getCode().equals("200")) {
                            flag = data.getFlag();
                            datelist.addAll(data.getData());
                            adapter.setNewData(datelist);
                            adapter.notifyDataSetChanged();

                        }

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        adapter = new BaseQuickAdapter<CompanyNaturalPublistBean.DataBean, BaseViewHolder>(R.layout.item_ziyuangongxiang, datelist) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CompanyNaturalPublistBean.DataBean bean) {
                GlideApp.with(IntelligenceSecondActivity.this).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.mipmap.logo_qyzp).error(R.mipmap.logo_qyzp)).into((ImageView) holder.getView(R.id.img));
                holder.setText(R.id.tv_title, bean.getTitle());
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (getIntent().getStringExtra("id").equals("48")){
                    startActivity(new Intent(IntelligenceSecondActivity.this, IntelligenceDetailsActivity.class).putExtra("info_id", datelist.get(position).getInfo_id() + "").putExtra("type_id",getIntent().getStringExtra("id")));

                }else {
                    startActivity(new Intent(IntelligenceSecondActivity.this, WebVIewActivity.class).putExtra("info_id", datelist.get(position).getInfo_id() + "").putExtra("type_id",getIntent().getStringExtra("id")));


                }

            }
        });
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                Log.e("TAG", "onRefresh:---- " );
                super.onRefresh(refreshLayout);
//                new Handler().postDelayed(() -> {

                mPage = 1;
                datelist.clear();
                company_natural_publist();
                refreshLayout.finishRefreshing();
//                }, 1000);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);



                if (flag.equals("T")) {
                    mPage++;
                    company_natural_publist();
                } else {

                }
                refreshLayout.finishLoadmore();
            }
        });
        showEmptyView(adapter);
        etSearch.setOnEditorActionListener(this);
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 模拟点击登录按钮

            mPage = 1;
            datelist.clear();
            company_natural_publist();
            hideKeyboard(getCurrentFocus());
//            搜索


        }
        return  false;
    }

    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {

    }

    @Override
    public void onSoftKeyboardClosed() {

    }
}
